module System.Serializer;
import std.string;
//import std.uri;

public interface ISerializer {
	void deserialize( SerialData s);
	SerialData serialize();
	static ISerializer create( SerialData s );
} //




public class SerialData{
	private string _data;
	private string[] _params;
	private int _ptr ;


	public this( string data){
		_data = decode( data );
		_params = split( _data , "," );
	} //

	public string data(){
		return encode( _data );
	} //

	public bool hasNext() {
		return _ptr < _params.length ;
	} //

	private string param(){
		if ( _ptr >= _params.length ) return null;
		return _params[ _ptr++ ] ;
	} //

	public string getString(T)( string def ) {
		string src = param;
		if ( src is null ) return def;
		return src;
	} //

	public T getNumber(T)( T def ) {
		try{
			string src = param;
			if ( src is null ) return def;
			return cast( T ) atof( src );
		} catch ( StringException s ) {
			return def;
		}
	} //

	public T getInt(T)( T def ) {
		try{
			string src = param;
			if ( src is null ) return def;
			return cast( T ) atoi( src );
		} catch ( StringException s ) {
			return def;
		}
	} //

	public ISerializer getObject( ISerializer delegate( SerialData s ) create ) {
		string src = param;
		if ( src is null ) return create( null );
		return create( new SerialData( src ) );
	} //



	public static string encode( string str){
		string  tmp = replace(str , "%" , "%25" );
				tmp = replace(tmp , "," , "%2c" );
		return tmp;
	} //
	public static string decode( string str){
		string  tmp = replace(str , "%2c" , "," );
				tmp = replace(tmp , "%25" , "%" );
		return tmp;
	} //
	
} //
